package pers.cz.queue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 设置公共队列存储存储对象
 * @program: PostGirl-panent
 * @description: RequestQueue
 * @author: Cheng Zhi
 * @create: 2021-03-07 19:20
 **/
public class  RequestQueue {

    private static Queue queue = new LinkedBlockingQueue();

    /**
     * 添加元素到队列
     * @param object
     * @return
     */
    public static void addQueue(Object object) {
        queue.offer(object);
    }

    /**
     * 判断是否为空队列，true表示空队列，false表示非空
     * @return
     */
    public static boolean isEmptyQueue() {

        if (queue.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * 获取队首
     * @return
     */
    public static Object getHeadQueue() {

        Object o = queue.peek();
        return o;
    }

    /**
     * 删除队首
     */
    public static boolean deleteHeadQueue() {

        try {
            queue.poll();
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * 获取队列大小
     * @return
     */
    public static Integer getQueueSize() {

        return queue.size();
    }

    /**
     * 传入List集合返回无界队列
     * @param object
     * @return
     */
    public static Queue<Object> returnQueue(List<Object> object) {

        // 定义一个无界队列
        BlockingQueue<Object> blockingQueue = new LinkedBlockingQueue<>();
        blockingQueue.addAll(object);

        return blockingQueue;
    }

    public static Queue<Object> returnQueue(List<Object> objects, int size) {

        // 定义一个无界队列
        BlockingQueue<Object> blockingQueue = new LinkedBlockingQueue<>();
        for (Object o :objects) {
            blockingQueue.add(o);
        }
        return blockingQueue;
    }

    public static void main(String[] args) {
        List<String> testList = new ArrayList<>();
        testList.add("1");
        testList.add("2");
        testList.add("3");
        testList.add("4");
        Queue queue = returnQueue(Collections.singletonList(testList),1);

        System.out.println(queue.poll());

    }
}
